#### Covid-19 Spillover Effects onto General Vaccine Attitudes
### Replication code - Flu shot over time - Figure 3 
### use file "wavesmany_cl.csv"
### Kristin Lunz Trujillo

library(dplyr)
library(haven)
library(srvyr)
library(survey)
library(tidyr)
library(broom)
library(purrr)
library(ggplot2)
library(stargazer)
library(dotwhisker)
library(tibble)
library(xtable)
library(jtools)
library(Hmisc)
library(ggpubr)

## load data
wavesmany_cl <- read.csv(".../wavesmany_cl.csv")

# data cleaning
wavesmany_cl <- wavesmany_cl %>% 
  mutate(
    flushotgot = case_when(
      flu_shot2 == 1 ~ 1,
      flu_shot2 > 1 ~ 0),
    flushotint = case_when(
      flu_shot3 < 3 ~ 1,
      flu_shot3 > 2 ~ 0
    ),
    vaxhes1 = case_when(vac_gen_1 == 1 ~ 7,
                        vac_gen_1 == 2 ~ 6,
                        vac_gen_1 == 3 ~ 5,
                        vac_gen_1 == 5 ~ 3,
                        vac_gen_1 == 6 ~ 2,
                        vac_gen_1 == 7 ~ 1),
    vaxhes2 = vac_gen_2,
    vaxhes3 = case_when(vac_gen_3 == 1 ~ 7,
                        vac_gen_3 == 2 ~ 6,
                        vac_gen_3 == 3 ~ 5,
                        vac_gen_3 == 5 ~ 3,
                        vac_gen_3 == 6 ~ 2,
                        vac_gen_3 == 7 ~ 1),
    pid3 = case_when(party7 > 4 ~ "Democrat",
                     party7 < 4 ~ "Republican",
                     party7 == 4 ~ "Independent")
  )
wavesmany_cl <- wavesmany_cl %>% 
  mutate(
    flushotall = case_when(flushotgot == 1 ~ 1,
                           flushotgot == 0 ~ 0,
                           flushotint == 1 ~ 1,
                           flushotint == 0 ~ 0))
wavesmany_cl$vaxhes = ((wavesmany_cl$vaxhes1+wavesmany_cl$vaxhes2+wavesmany_cl$vaxhes3)/3)

wavesmany_cl$insttrust = (wavesmany_cl$inst_trust1_7+wavesmany_cl$inst_trust2_2+wavesmany_cl$inst_trust2_3)
table(wavesmany_cl$insttrust)

wavesmany_cl <- wavesmany_cl %>%
  mutate(
    insttrust_step = case_when(insttrust == 10 ~ "A. High Trust",
                               insttrust == 12 ~ "A. High Trust",
                               insttrust == 11 ~ "A. High Trust",
                               insttrust == 8 ~ "B. Medium Trust",
                               insttrust == 9 ~ "B. Medium Trust",
                               insttrust == 7 ~ "B. Medium Trust",
                               insttrust == 4 ~ "C. Low Trust",
                               insttrust == 6 ~ "C. Low Trust",
                               insttrust == 5 ~ "C. Low Trust",
                               insttrust == 3 ~ "C. Low Trust"),
    date = case_when(study == 7 ~ "Dec 20",
                     study == 9 ~ "Feb 21",
                     study == 10 ~ "Apr 21",
                     study == 11 ~ "Jun 21",
                     study == 20 ~ "Nov 21",
                     study == 21 ~ "Jan 22",
                     study == 22 ~ "Mar 22")
  )

wavesmany_cl <- wavesmany_cl %>%
  mutate(
    beforeaft = case_when(study < 10 ~ "1) Received Flu Shot (July/August 2020)",
                          study == 14 ~ "2) Flu Shot Intention (Winter 2020/21)",
                          study == 16 ~ "2) Flu Shot Intention (Winter 2020/21)",
                          study > 16 ~ "3) Flu Shot Intention (Winter 2021/22)"))

table(wavesmany_cl$study)

# wave date labels
wavesmany_cl$date <- factor(wavesmany_cl$date, levels=c("July 20", "Aug 20", "Sep 20", "Dec 20", "Feb 21", "Sept 21", "Dec 21", "Jan 22", "Mar 22"))


# labels for x axis flu shot
my.labels <- c("1) Received Flu Shot \n(July/August 2020)", 
               "2) Flu Shot Intention \n(Winter 2020/21)",
               "3) Flu Shot Intention \n(Winter 2021/22)")




###################################################################################
## Figure: Flu Shot and prop. who said yes (Figure 3)
wavesmany_cl %>%
  filter(!is.na(flushotall)) %>%
  filter(!is.na(beforeaft)) %>%
  filter(!is.na(pid3)) %>%
  ggplot(aes(beforeaft,flushotall))+     
  stat_summary(geom = "line", fun = mean, position = "dodge") +
  stat_summary(geom = "point", fun = mean, position = "dodge") +
  stat_summary(fun.data = mean_cl_normal,  
               geom = "errorbar") + facet_wrap(~pid3) + theme_bw() + scale_y_continuous(name="Proportion Who Said Yes", limits=c(0, 1)) +
  scale_x_discrete(name="", labels=my.labels)  + theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1, size=12), axis.title.y=element_text(size=12),
                                                       strip.text.x = element_text(size = 14))









